<?php include("includes/startPage.php"); checkLogin(0); ?>
<?php include('includes/header.php'); printHeader("Browse", "Nordic Sauna");?> <!-- (PageTitle, Banner Text) Prints <head>, opens <body> and prints banner -->
<?php include('includes/navigation.php'); printLeftMenu("software"); ?> <!-- Navigation include, (currentpagename) -->

<?php OpenContentSection(); ?> 
	<!-- Content Starts -->
            <?php
                $software = ns_dao_software::GetSoftwareDescription($_GET["id"]);
		$version = ns_dao_software::SoftwareVersionLatestBySoftwareID2($_GET['id']);				
                $owner = $software->Owner();
                $owningUser = ns_dao_user::UserByUsername($owner);
                
                echo "<h2>" . $software->Name() . "</h2>";
                echo '<img src="../../content/softwareAvatar/'.$software->AvatarImage().'" width="160px" height="160px">';
                echo "<p><strong>Uploaded By:</strong> " . $owningUser->FirstName() . " " . $owningUser->LastName() . "</p>";

                echo "<p>" . $software->Description() . "</p>";

                $version_msg = '<strong>Please register/login to download this software!</strong>';
                if (isset($_SESSION['username']))
                {
                    $version_msg = 'No version of this software';

                    if (!is_null($version))
                    {
                        $version_msg = '<strong>Files in version '.$version[0]["VersionNumber"].':</strong><br />';
                        $dirName = "../../content/software/" . $_GET['id'] . "/" . $version[0]["SoftwareVersionID"];
                        $dir = opendir($dirName);
                        $fileList = array();
                        
                        while (false != ($file = readdir($dir)))
                        {
                            if ($file != "." && $file != "..")
                            {
                                array_push($fileList, $file);
                                //array_push($fileList, $file);
                                //array_push($fileList, '<a href="' . $dirName . '/'. $file . '">' . $file . '</a>');
                            }
                        }
                        closedir($dir);		
                        sort($fileList);
                    				
                        echo $version_msg;

                        if (!is_null($fileList))
                        {
                            //echo '<form name="downloadSoftware" id="downloadSoftware" action="download_software.php" method="GET" >';
                            //echo '<input type="hidden" name="user" id="user" value="'.$_SESSION['username'].'">';
                            //echo '<input type="hidden" name="softwareID" id="softwareID" value="'.$_GET['id'].'">';
                            //echo '<input type="hidden" name="softwareVersionID" id="softwareVersionID" value="'.$version[0]["SoftwareVersionID"].'">';
                            for ($i = 0; $i < count($fileList); $i++)
                            {
                                //echo '<input type="hidden" name="i" id="i" value="'.$i.'">';
                                //echo '<input type="hidden" name="filename'.$i.'" id="filename'.$i.'" value="'.$fileList[$i].'">';
                                //echo '<a href="javascript: document.downloadSoftware.submit()" >'.$fileList[$i].'</a><br />';
                                //echo '<a href="download_software.php?i='.$i.'">'.$fileList[$i].'</a><br />';                                
                                
                                $link = '<a href="download_software.php?';
                                $link .= 'user=' . $_SESSION['username'];
                                $link .= '&softwareID=' . $_GET['id'];
                                $link .= '&softwareVersionID=' . $version[0]["SoftwareVersionID"];
                                $link .= '&filename=' . $fileList[$i];
                                $link .= '">'.$fileList[$i].'</a><br />';
                                echo $link;
                            }
                            //echo '</form>';
                        }
                    }
                } else {
                    echo $version_msg;
                }
            ?>        
	<!-- Content Ends -->
<?php CloseContentSection(); ?> 
<?php include('includes/footer.php');?>